import { Router, Request } from "express";
import { prisma } from "./db";
import { authList } from "./ram";
import { Pagination } from "./types";

export const selectrouter = Router();

selectrouter.post("/user", async (req: Request<{}, {}, Pagination>, res) => {
    if (authList[req.cookies.auth] !== undefined && authList[req.cookies.auth] !== undefined) {
        const list = await prisma.user.findMany({ include: { prepodavatel: true, student: true }, skip: ((req.body.page || 0) - 1) * (req.body.size || 100) });
        res.json({ res: "ok", list: list });
    } else
        res.json({ res: "bad", text: "Авторизация не пройдена" });
});
selectrouter.post("/userCount", async (req: Request<{}, {}, Pagination>, res) => {
    if (authList[req.cookies.auth] !== undefined && authList[req.cookies.auth] !== undefined) {
        const count = await prisma.user.count({ });
        res.json({ res: "ok", count });
    } else
        res.json({ res: "bad", text: "Авторизация не пройдена" });
});

selectrouter.post("/otdelenie", async (req: Request<{}, {}, Pagination>, res) => {
    if (authList[req.cookies.auth] !== undefined && authList[req.cookies.auth] !== undefined) {
        const list = await prisma.otdelenie.findMany({ include: { director: true, groups: true }, skip: ((req.body.page || 0) - 1) * (req.body.size || 100) });
        res.json({ res: "ok", list: list });
    } else
        res.json({ res: "bad", text: "Авторизация не пройдена" });
});

selectrouter.post("/otdelenieCount", async (req: Request<{}, {}, Pagination>, res) => {
    if (authList[req.cookies.auth] !== undefined && authList[req.cookies.auth] !== undefined) {
        const count = await prisma.otdelenie.count({ });
        res.json({ res: "ok", count });
    } else
        res.json({ res: "bad", text: "Авторизация не пройдена" });
});

selectrouter.post("/group", async (req: Request<{}, {}, Pagination>, res) => {
    if (authList[req.cookies.auth] !== undefined && authList[req.cookies.auth] !== undefined) {
        const list = await prisma.group.findMany({ include: { curator: true, otdelenie: true, students: true }, skip: ((req.body.page || 0) - 1) * (req.body.size || 100) });
        res.json({ res: "ok", list: list });
    } else
        res.json({ res: "bad", text: "Авторизация не пройдена" });
});

selectrouter.post("/groupCount", async (req: Request<{}, {}, Pagination>, res) => {
    if (authList[req.cookies.auth] !== undefined && authList[req.cookies.auth] !== undefined) {
        const count = await prisma.group.count({ });
        res.json({ res: "ok", count });
    } else
        res.json({ res: "bad", text: "Авторизация не пройдена" });
});

selectrouter.post("/student", async (req: Request<{}, {}, Pagination>, res) => {
    if (authList[req.cookies.auth] !== undefined && authList[req.cookies.auth] !== undefined) {
        const list = await prisma.student.findMany({ include: { group: true, contracts: true, user: true }, skip: ((req.body.page || 0) - 1) * (req.body.size || 100) });
        res.json({ res: "ok", list: list });
    } else
        res.json({ res: "bad", text: "Авторизация не пройдена" });
});

selectrouter.post("/studentCount", async (req: Request<{}, {}, Pagination>, res) => {
    if (authList[req.cookies.auth] !== undefined && authList[req.cookies.auth] !== undefined) {
        const count = await prisma.student.count({ });
        res.json({ res: "ok", count });
    } else
        res.json({ res: "bad", text: "Авторизация не пройдена" });
});

selectrouter.post("/prepodavatel", async (req: Request<{}, {}, Pagination>, res) => {
    if (authList[req.cookies.auth] !== undefined && authList[req.cookies.auth] !== undefined) {
        const list = await prisma.prepodavatel.findMany({ include: { group: true, otdelenie: true, user: true }, skip: ((req.body.page || 0) - 1) * (req.body.size || 100) });
        res.json({ res: "ok", list: list });
    } else
        res.json({ res: "bad", text: "Авторизация не пройдена" });
});

selectrouter.post("/prepodavatelCount", async (req: Request<{}, {}, Pagination>, res) => {
    if (authList[req.cookies.auth] !== undefined && authList[req.cookies.auth] !== undefined) {
        const count = await prisma.prepodavatel.count({ });
        res.json({ res: "ok", count });
    } else
        res.json({ res: "bad", text: "Авторизация не пройдена" });
});

selectrouter.post("/room", async (req: Request<{}, {}, Pagination>, res) => {
    if (authList[req.cookies.auth] !== undefined && authList[req.cookies.auth] !== undefined) {
        const list = await prisma.room.findMany({ include: { contracts: true, items: true }, skip: ((req.body.page || 0) - 1) * (req.body.size || 100) });
        res.json({ res: "ok", list: list });
    } else
        res.json({ res: "bad", text: "Авторизация не пройдена" });
});

selectrouter.post("/roomCount", async (req: Request<{}, {}, Pagination>, res) => {
    if (authList[req.cookies.auth] !== undefined && authList[req.cookies.auth] !== undefined) {
        const count = await prisma.room.count({ });
        res.json({ res: "ok", count });
    } else
        res.json({ res: "bad", text: "Авторизация не пройдена" });
});

selectrouter.post("/inventarycategory", async (req: Request<{}, {}, Pagination>, res) => {
    if (authList[req.cookies.auth] !== undefined && authList[req.cookies.auth] !== undefined) {
        const list = await prisma.inventaryCategory.findMany({ include: { items: true }, skip: ((req.body.page || 0) - 1) * (req.body.size || 100) });
        res.json({ res: "ok", list: list });
    } else
        res.json({ res: "bad", text: "Авторизация не пройдена" });
});

selectrouter.post("/inventarycategoryCount", async (req: Request<{}, {}, Pagination>, res) => {
    if (authList[req.cookies.auth] !== undefined && authList[req.cookies.auth] !== undefined) {
        const count = await prisma.inventaryCategory.count({ });
        res.json({ res: "ok", count });
    } else
        res.json({ res: "bad", text: "Авторизация не пройдена" });
});

selectrouter.post("/inventary", async (req: Request<{}, {}, Pagination>, res) => {
    if (authList[req.cookies.auth] !== undefined && authList[req.cookies.auth] !== undefined) {
        const list = await prisma.inventary.findMany({ include: { category: true }, skip: ((req.body.page || 0) - 1) * (req.body.size || 100) });
        res.json({ res: "ok", list: list });
    } else
        res.json({ res: "bad", text: "Авторизация не пройдена" });
});

selectrouter.post("/inventaryCount", async (req: Request<{}, {}, Pagination>, res) => {
    if (authList[req.cookies.auth] !== undefined && authList[req.cookies.auth] !== undefined) {
        const count = await prisma.inventary.count({ });
        res.json({ res: "ok", count });
    } else
        res.json({ res: "bad", text: "Авторизация не пройдена" });
});

selectrouter.post("/contract", async (req: Request<{}, {}, Pagination>, res) => {
    if (authList[req.cookies.auth] !== undefined && authList[req.cookies.auth] !== undefined) {
        const list = await prisma.contract.findMany({ include: { payments: true, room: true, student: true }, skip: ((req.body.page || 0) - 1) * (req.body.size || 100) });
        res.json({ res: "ok", list: list });
    } else
        res.json({ res: "bad", text: "Авторизация не пройдена" });
});

selectrouter.post("/contractCount", async (req: Request<{}, {}, Pagination>, res) => {
    if (authList[req.cookies.auth] !== undefined && authList[req.cookies.auth] !== undefined) {
        const count = await prisma.contract.count({ });
        res.json({ res: "ok", count });
    } else
        res.json({ res: "bad", text: "Авторизация не пройдена" });
});

selectrouter.post("/payment", async (req: Request<{}, {}, Pagination>, res) => {
    if (authList[req.cookies.auth] !== undefined && authList[req.cookies.auth] !== undefined) {
        const list = await prisma.payment.findMany({ include: { contract: true }, skip: ((req.body.page || 0) - 1) * (req.body.size || 100) });
        res.json({ res: "ok", list: list });
    } else
        res.json({ res: "bad", text: "Авторизация не пройдена" });
});

selectrouter.post("/paymentCount", async (req: Request<{}, {}, Pagination>, res) => {
    if (authList[req.cookies.auth] !== undefined && authList[req.cookies.auth] !== undefined) {
        const count = await prisma.payment.count({ });
        res.json({ res: "ok", count });
    } else
        res.json({ res: "bad", text: "Авторизация не пройдена" });
});
